/*
 * Project: Sudoku Explainer
 * Copyright (C) 2006-2007 Nicolas Juillerat
 * 
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.rules.uniqueness;

import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.onegravity.sudoku.solver.HintResource;
import com.onegravity.sudoku.solver.HintType;
import com.onegravity.sudoku.solver.R;
import com.onegravity.sudoku.solver.core.Rule;
import com.onegravity.sudoku.solver.data.Cell;
import com.onegravity.sudoku.solver.data.ImmutableCell;
import com.onegravity.sudoku.solver.util.HtmlLoader;
import com.onegravity.sudoku.solver.util.BitSetFactory;
import com.onegravity.sudoku.solver.util.ValuesFormatter;

public class Bug2Hint extends BugHint implements Rule {

	private static final long serialVersionUID = -6304431219360762228L;
	
	private final Cell[] bugCells;
    private final int bugValue;

    public Bug2Hint(Map<Cell, BitSet> removablePotentials, Cell[] bugCells, int bugValue) {
        super(removablePotentials);
        this.bugCells = bugCells;
        this.bugValue = bugValue;
    }

    @Override
    public Collection<Cell> getSelectedCells(HintType hintType) {
    	return (hintType!=HintType.SMALL_CLUE) ? Arrays.asList(bugCells) : null;
    }

    @Override
    public Map<Cell, BitSet> getAddedPotentials(HintType hintType, int viewNum) {
        Map<Cell, BitSet> result = new HashMap<Cell, BitSet>();
        for (Cell cell : bugCells) {
            result.put(cell, BitSetFactory.createOneElement(bugValue));
        }
        return (hintType==HintType.HINT) ? result : null;
    }

    @Override
    public Map<ImmutableCell, BitSet> getRemovedPotentials(HintType hintType) {
        return (hintType==HintType.HINT) ? super.getRemovedPotentials(hintType) : null;
    }

    @Override
    public double getDifficulty() {
        return 5.7;
    }

    @Override
    protected HintResource getResource() {
    	return HintResource.BUG_2;
    }

    @Override
    protected int getType() {
    	return 2;
    }

    @Override
    protected String getAppendixString() {
    	return Cell.toString(bugCells) + " " + HintResource.getString(R.string.on) + " " + bugValue; //$NON-NLS-1$ //$NON-NLS-2$
    }

    @Override
    public String getHintHtml(HintType hintType) {
        String result = HtmlLoader.loadHtml(HintResource.BUG_2.hint(hintType));
        String andBugCells = ValuesFormatter.formatCells(bugCells, " " + HintResource.getString(R.string.and) + " "); //$NON-NLS-1$ //$NON-NLS-2$
        return HtmlLoader.format(result, bugValue, andBugCells, bugCells.length);
    }

}
